package com.lzs.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Area;
import com.lzs.PO.Mechanism;
import com.lzs.PO.Page;
import com.lzs.Service.MechanismService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/mechanism")
public class MechanismController {
	@Autowired
	MechanismService mechanismService;

	@RequestMapping(value = "/mechanism_list")
	public String mechanism_list(Model model, HttpServletRequest request,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			String keyword) throws Exception {

		Page<Mechanism> page = this.mechanismService.findMechanismPagelist(keyword, pageSize, pageNum);

		model.addAttribute("page", page);
        System.out.println(page.getList());
		return "/mechanism/mechanism_list";
	}

	@RequestMapping(value = "/mechanism_add")
	public String addMechanism() {

		return "/mechanism/mechanism_add";
	}

	@ResponseBody
	@RequestMapping(value = "/mechanism_add_action")
	public Msg addMechanismAction(String mechanism_id, String mechanism_name, String mechanism_rank, String area_id) {

		try {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("mechanism_id", mechanism_id);

			map.put("mechanism_name", mechanism_name);

			map.put("mechanism_rank", mechanism_rank);

			map.put("area_id", area_id);

			this.mechanismService.addMechanism(map);

			return Msg.success();

		} catch (Exception e) {

			return Msg.fail();

		}
	}
	@ResponseBody
	@RequestMapping(value = "/mechanism_check")
	public Msg checkMechanism(String mechanism_name) {
		
		Boolean b = this.mechanismService.countMechanismByName(mechanism_name);
		
		if(b) {
			
			return Msg.success();
		}else {
			
			return Msg.fail();
		}
	}
	@RequestMapping(value = "/mechanism_update")
	   public String updateMechanism(String mechanism_id,Model model) {
		  
		     Mechanism mechanism = this.mechanismService.findMechanismById(mechanism_id);
		     
		     model.addAttribute("mechanism", mechanism);
		     
		     return"/mechanism/mechanism_update";
		
	}
	@ResponseBody
	@RequestMapping(value = "mechanism_update_action")
	public Msg updateMechanism(String mechanism_id, String mechanism_name, String area_id, String mechanism_rank) {
		      
		try {
			Map<String,Object>map = new HashMap<String,Object>();
			
			map.put("mechanism_id", mechanism_id);
			
			map.put("mechanism_name", mechanism_name);
			
			map.put("area_id", area_id);
			
			map.put("mechanism_rank", mechanism_rank);
			
			this.mechanismService.updateMechanismById(map);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			return Msg.fail();
			
		}
	}
	@ResponseBody
	@RequestMapping(value = "/mechanism_delete")
	public Msg deleteMchanismById(String mechanism_id) {
		
		try {
			
			this.mechanismService.deleteMechanismById(mechanism_id);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
		
	}
}
